import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    houseId:'',//房屋的ID
    moblie:'',//手机号
    houseInfo:'',//房屋名称
    repairItemId: '',//维修项目的Id
    repairItemName:'',//维修项目名称
    appointment: '',//预约-时间
    description: ''//问题的描述
  },
  rules: {
    houseId: [
      {required: true, message: '请选择报修房屋!'}
    ],
    repairItemId: [
      {required: true, message: '请选择维修的项目!'}
    ],
    mobile: [
      {required: true, message: '请填写手机号码!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'},
    ],
    appointment: [
      {required: true, message: '请选择预约日期!'}
    ],
    description: [
      {required: true, message: '请填写问题描述!'}
    ],
  },
  onLoad({id}){
    this.getHouseList()
    this.getRepairList()
    if(id){
      this.getRepairInfo(id)
      wx.setNavigationBarTitle({
        title:'修改报修信息'
      })
    }
  },
// 获取修改的报修信息
async getRepairInfo(id) {
  // 调用接口
let res = await wx.http.get('/repair/' + id)
  // 渲染数据
  this.setData({ ...res })
},
// 选择了日期
selectDateInfo(event){
    this.setData({
      appointment:wx.utils.dataFormat(event.detail),
      dateLayerVisible:false
    })
  },
  // 获取到我的房屋信息
  async getHouseList(){
    let houseList = await wx.http.get('/room')
    // console.log(houseList);
    houseList= houseList.map(item =>({
      name: item.point+ item.building + item.room,
      id:item.id
    }))
    this.setData({houseList})
  },
    // 选择房屋
    selectHouse({ detail }){
      this.setData({ houseId: detail.id, houseInfo: detail.name})
    },



  // 获取维修项目列表请求
  async getRepairList(){
    let repairItem = await wx.http.get('/repairItem')
    this.setData({repairItem})
  },
  // 选择维修项目
selectRepair({detail}){
  this.setData({ repairItemId: detail.id, repairItemName: detail.name})
},
// 选择图片事件
afterRead(event){
  const {file} = event.detail
  wx.uploadFile({
    url: wx.http.baseURL + '/upload',
    filePath: file.url,
    name: 'file',
    header: {
      Authorization: 'Bearer ' + getApp().token,
    },
    success: (res) => {
      const result = res.data
      // 处理返回的 json 数据
      const { code, message, data} = JSON.parse(result)
      // 检测接口是否调用成功
      if (code !== 10000) return wx.utils.toast(message)
    
      // 渲染数据
      this.setData({ 
        attachment:[...this.data.attachment,{url: data.url}]
       })
    },
  })
  
},
// 删除图片
deleteImg(event){
  this.setData({
    attachment: this.data.attachment.filter((item, index)=>{
      return index !== event.detail.index
    })
  })
},
// 打开选择弹窗
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  // 关闭选择弹窗
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // 打开选择维修项目列表
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
   // 关闭选择维修项目列表
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 表单数据的提交
   async submitOk(){
    // 表单的数据校验
    const isPass= this.validate()
    if(!isPass) return 
    const { id, houseId, repairItemId, mobile, appointment, description, attachment} = this.data
    // 调用接口
    await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
    })
    // 检测接口是否调用成功
     wx.utils.toast('操作成功')
    // 跳转到报修列表页面
    setTimeout(()=>{
      this.goList()
    },300)

  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
